package org.kernelab.numeric.apps;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.kernelab.matrix.Matrix;

public class Launcher extends JDialog
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -6113803817029884593L;

	public static int			LEFT				= 200;

	public static int			TOP					= 150;

	public static int			WIDTH				= 300;

	public static int			HEIGHT				= 200;

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{

	}

	private AppParser	app;

	private Matrix		matrix;

	private JLabel		nameLabel;

	private JLabel		studio;

	private JLabel		status;

	private JLabel		version;

	public Launcher(AppParser app)
	{
		super();

		this.app = app;

		this.matrix = new Matrix(new Dimension(Launcher.WIDTH, Launcher.HEIGHT), 3000,
				Color.GREEN, Color.BLACK, false);

		this.nameLabel = new JLabel("Algebra");

		this.studio = new JLabel("KerneLab.org");

		this.status = new JLabel("正在加载软件...");

		this.version = new JLabel("Version: " + Parser.VERSION);

		this.config();

		this.arrange();

	}

	private void arrange()
	{
		this.setContentPane(this.matrix.getPanel());

		this.matrix.start();

		JPanel glass = new JPanel();

		this.setGlassPane(glass);

		glass.setOpaque(false);

		glass.setLayout(new GridBagLayout());

		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.SOUTH;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridheight = 1;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.ipadx = 0;
		gbc.ipady = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;

		glass.add(this.nameLabel, gbc);

		gbc.anchor = GridBagConstraints.NORTH;
		gbc.gridy++;
		gbc.insets = new Insets(5, 100, 0, 0);
		glass.add(this.studio, gbc);

		gbc.gridwidth = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbc.weighty = 0.0;
		gbc.anchor = GridBagConstraints.SOUTHWEST;
		gbc.gridy++;
		gbc.insets = new Insets(1, 3, 1, 3);
		glass.add(this.status, gbc);

		gbc.anchor = GridBagConstraints.SOUTHEAST;
		gbc.gridx++;
		glass.add(this.version, gbc);

		glass.setVisible(true);

		this.pack();

		this.setBounds(Launcher.LEFT, Launcher.TOP, Launcher.WIDTH, Launcher.HEIGHT);

		this.setVisible(true);
	}

	public void close()
	{
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		this.setVisible(false);

		this.matrix.stop();

		try {
			this.finalize();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	private void config()
	{
		this.setUndecorated(true);

		this.setAlwaysOnTop(true);

		this.getContentPane().setBackground(Color.WHITE);

		this.nameLabel.setHorizontalAlignment(JLabel.CENTER);
		this.nameLabel.setFont(new Font("Verdana", Font.BOLD, 30));
		this.nameLabel.setOpaque(false);
		this.nameLabel.setForeground(Color.WHITE);

		this.studio.setHorizontalAlignment(JLabel.CENTER);
		this.studio.setFont(new Font("Arial", Font.BOLD, 12));
		this.studio.setForeground(Color.WHITE);

		this.status.setForeground(Color.WHITE);
		this.version.setForeground(Color.WHITE);
	}

	public AppParser getApp()
	{
		return app;
	}

	public void setStatus(String status)
	{
		this.status.setText(status);
	}

}
